package com.element.security.resource.handler;

import com.common.core.exception.code.SecurityErrorCode;
import com.common.core.results.VoidResult;
import com.element.security.utils.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 如果身份验证失败时调用
 *
 * @auther zhangwj
 * @date 2021/3/18 下午1:47
 */
public class GoAuthenticationFailureHandler implements AuthenticationFailureHandler {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public void onAuthenticationFailure(HttpServletRequest request,
                                        HttpServletResponse response,
                                        AuthenticationException e) throws IOException {
        logger.error("AuthenticationException异常,请求路径:{},异常信息:", request.getServletPath(), e);
        ResponseUtil.writeJson(response, VoidResult.failed(SecurityErrorCode.A_VALIDATION_FAILED));
    }
}
